package group.igfd.manager.biz.service.impl;

import group.igfd.manager.biz.entity.FreightcompanyEntity;
import group.igfd.manager.biz.entity.OrderItemEntity;
import group.igfd.manager.biz.entity.OrdersEntity;
import group.igfd.manager.biz.service.FreightcompanyService;
import group.igfd.manager.biz.service.OrderItemService;
import group.igfd.manager.biz.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import group.igfd.manager.biz.dao.TransportLogisticsDao;
import group.igfd.manager.biz.entity.TransportLogisticsEntity;
import group.igfd.manager.biz.service.TransportLogisticsService;
import org.springframework.transaction.annotation.Transactional;


@Service("transportLogisticsService")
public class TransportLogisticsServiceImpl extends ServiceImpl<TransportLogisticsDao, TransportLogisticsEntity> implements TransportLogisticsService {

    @Autowired
    private FreightcompanyService freightcompanyService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private OrdersService ordersService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<TransportLogisticsEntity> page = this.selectPage(
                new Query<TransportLogisticsEntity>(params).getPage(),
                new EntityWrapper<TransportLogisticsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public boolean add(Integer orderItemId, Integer freightId, String waybillNumber) throws Exception {
        TransportLogisticsEntity transportLogisticsEntity = new TransportLogisticsEntity();

        transportLogisticsEntity.setOrderItemId(orderItemId);

        EntityWrapper<FreightcompanyEntity> freightcompanyEntityEntityWrapper = new EntityWrapper<>();
        freightcompanyEntityEntityWrapper.eq("id",freightId);
        FreightcompanyEntity freightcompanyEntity = freightcompanyService.selectOne(freightcompanyEntityEntityWrapper);

        transportLogisticsEntity.setCompany(freightcompanyEntity.getCompany());
        transportLogisticsEntity.setCompanyCode(freightcompanyEntity.getCompanycode());
        transportLogisticsEntity.setWaybillNumber(waybillNumber);
        transportLogisticsEntity.setCreatedAt(new Date());
        transportLogisticsEntity.setUpdatedAt(new Date());

        boolean insert = this.insert(transportLogisticsEntity);

        if (insert) {
            EntityWrapper<OrderItemEntity> orderItemEntityEntityWrapper = new EntityWrapper<>();
            orderItemEntityEntityWrapper.eq("id",orderItemId);
            OrderItemEntity orderItemEntity = orderItemService.selectOne(orderItemEntityEntityWrapper);

            Integer orderId = orderItemEntity.getOrderId();

            EntityWrapper<OrderItemEntity> ordersEntityEntityWrapper = new EntityWrapper<>();
            ordersEntityEntityWrapper.eq("order_id",orderId);
            List<OrderItemEntity> orderItemEntities = orderItemService.selectList(ordersEntityEntityWrapper);

            boolean uploadAll = true;
            for (OrderItemEntity orderItem :orderItemEntities) {
                EntityWrapper<TransportLogisticsEntity> transportLogisticsEntityEntityWrapper = new EntityWrapper<>();
                transportLogisticsEntityEntityWrapper.eq("order_item_id",orderItem.getId());
                TransportLogisticsEntity transportLogisticsEntityFind = this.selectOne(transportLogisticsEntityEntityWrapper);

                if (transportLogisticsEntityFind == null) {
                    uploadAll = false;
                    break;
                }
            }

            if (uploadAll) {
                OrdersEntity ordersEntity = new OrdersEntity();

                ordersEntity.setId(orderId);
                ordersEntity.setStatus(4);
                boolean b = ordersService.updateById(ordersEntity);
                if (!b) {
                    throw new Exception("上传物流单号失败");
                }
            }
        }

        return true;
    }

}
